package com.android.juvid.testnet;

import android.app.Application;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import okhttp3.Call;
import okhttp3.OkHttpClient;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;
import android.app.DownloadManager.Request;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.Gson;
import java.util.List;
import java.util.ArrayList;

public class application extends Application {
    private static final String VALUE = "Harvey";

    private String value;
    public void onCreate() {
        Log.v("application","启动了");
        super.onCreate();

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
//                .addInterceptor(new LoggerInterceptor("TAG"))
                .connectTimeout(10000L, TimeUnit.MILLISECONDS)
                .readTimeout(10000L, TimeUnit.MILLISECONDS)
                //其他配置
                .build();

        OkHttpUtils.initClient(okHttpClient);
    }
    public void setValue(String value)
    {
        Log.v("application","开始调用接口");
        String url = "http://itunes.apple.com/lookup?id=1053417951";
        OkHttpUtils.get().url(url).build().execute(new StringCallback()
                {
                    String jsonStr="";
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.v("application","接口调用失败");
                    }
                    @Override
                    public void onResponse(String response, int id) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
//                            Map<String, Object> map = (Map)jsonObject;
//                            map=(Map)jsonObject;
//                            Log.v("%@",map.toString());

//                            Object juObj=jsonObject;
//                            int v = Log.v("juvid 对象", juObj["resultCount"]);
//                            for (int i=0;i<jsonObject.length();i++){
//                                String tpstr="";
//                                jsonStr="数量:"+jsonObject.getString("resultCount");
//                                JSONArray jsonArray5 = jsonObject.getJSONArray("results");
//                                for (int j=0;j<jsonArray5.length();j++){
//                                    JSONObject jsonOb5Son=(JSONObject)jsonArray5.opt(j);
//                                    tpstr="描述"+jsonOb5Son.getString("description");
////
//                                }
//                                jsonStr=jsonStr+tpstr;
//                            }
//                            Log.v("zms", jsonStr);
                        } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            Log.v("接口调用成功","解析失败");
                            e.printStackTrace();
                        }
                        Log.v("接口调用成功",response);

                    }
                });
        this.value = value;
    }
    public  void ObjectToJson(Object obj){
        Gson gson = new Gson();
        List<Person> persons = new ArrayList<Person>();
        for (int i = 0; i < 10; i++) {
            Person p = new Person();
            p.setName("name" + i);
            p.setAge(i * 5);
            persons.add(p);
        }
        String str = gson.toJson(persons);
    }

    public void  JsonToObject(String json){
        Gson gson = new Gson();
        Person person = gson.fromJson(json, Person.class);
    }

    public String getValue()    {
        return value;
    }
}
